package com.ssy.lingxi.member.merchant.serviceimpl.paas;

import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.member.merchant.api.constant.CountryCodeEnum;
import com.ssy.lingxi.member.merchant.api.model.vo.paas.CountryAreaVO;
import com.ssy.lingxi.member.merchant.api.model.vo.paas.CountryCodeUpdateInsertVO;
import com.ssy.lingxi.member.merchant.entity.CountryAreaDO;
import com.ssy.lingxi.member.merchant.entity.CountryCodeDO;
import com.ssy.lingxi.member.merchant.repository.CountryAreaRepository;
import com.ssy.lingxi.member.merchant.repository.CountryCodeRepository;
import com.ssy.lingxi.member.merchant.service.paas.IPaasCountryCodeAreaService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 国家代码、区域编码服务接口实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2020-07-14
 */
@Service
public class PaasCountryCodeAreaServiceImpl implements IPaasCountryCodeAreaService {
    @Resource
    private CountryCodeRepository countryCodeRepository;

    @Resource
    private CountryAreaRepository countryAreaRepository;

    /**
     * 新增或修改国家代码
     * @param codeVO 接口参数
     * @return 操作结果
     */
    @Override
    public Wrapper<Void> updateOrInsertCountryCode(CountryCodeUpdateInsertVO codeVO) {
        CountryCodeEnum countryCodeEnum = CountryCodeEnum.findByCode(codeVO.getCode());
        if(countryCodeEnum == null) {
            return Wrapper.fail(ResponseCode.REQUEST_PARAM_CHECK_FAILED, "手机号码前缀不存在");
        }

        CountryCodeDO countryCodeDO = countryCodeRepository.findFirstByCode(codeVO.getCode());
        if(countryCodeDO == null) {
            countryCodeDO = new CountryCodeDO();
        }


        countryCodeDO.setName(countryCodeEnum.getCn());
        countryCodeDO.setTypeEnum(countryCodeEnum.getTypeEnum());
        countryCodeDO.setCode(codeVO.getCode());
        countryCodeDO.setPhoneLength(codeVO.getPhoneLength());
        countryCodeRepository.saveAndFlush(countryCodeDO);

        return Wrapper.success();
    }

    /**
     * 删除国家代码
     *
     * @param codeVO 接口参数
     * @return 删除结果
     */
    @Override
    public Wrapper<Void> deleteCountryCode(CountryCodeUpdateInsertVO codeVO) {
        CountryCodeEnum countryCodeEnum = CountryCodeEnum.findByCode(codeVO.getCode());
        if(countryCodeEnum == null) {
            return Wrapper.fail(ResponseCode.REQUEST_PARAM_CHECK_FAILED, "手机号码前缀不存在");
        }

        CountryCodeDO countryCodeDO = countryCodeRepository.findFirstByCode(codeVO.getCode());
        if(countryCodeDO == null) {
            return Wrapper.success();
        }

        countryCodeRepository.delete(countryCodeDO);
        return Wrapper.success();
    }

    /**
     * 新增区域编码
     *
     * @param areaVOList 接口参数
     * @return 操作结果
     */
    @Override
    public Wrapper<Void> addCountryArea(List<CountryAreaVO> areaVOList) {
        if(CollectionUtils.isEmpty(areaVOList)) {
            return Wrapper.success();
        }

        List<CountryAreaDO> areaDOList = areaVOList.stream().sorted(Comparator.comparingInt(CountryAreaVO::getLevel)).map(areaVO -> {
            CountryAreaDO areaDO = new CountryAreaDO();
            areaDO.setCode(areaVO.getCode());
            areaDO.setName(areaVO.getName());
            areaDO.setLevel(areaVO.getLevel());
            areaDO.setParentCode(areaVO.getpCode());
            return areaDO;
        }).collect(Collectors.toList());

        countryAreaRepository.saveAll(areaDOList);
        return Wrapper.success();
    }
}
